﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 枚举拓展
/// </summary>
public static class EnumExtension
{
    /// <summary>
    /// 随机获取枚举值
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static T RandomEnum<T>()
    {
        T[] results = Enum.GetValues(typeof(T)) as T[];
        System.Random random = new System.Random(GetRandomSeed());
        T result = results[random.Next(0, results.Length)];
      
        return result;
    }
    /// <summary>
    /// 创建加密随机数生成器 生成强随机种子
    /// </summary>
    /// <returns></returns>
    private static int GetRandomSeed()
    {
        byte[] bytes = new byte[4];
        System.Security.Cryptography.RNGCryptoServiceProvider rng
            = new System.Security.Cryptography.RNGCryptoServiceProvider();
        rng.GetBytes(bytes);
        return BitConverter.ToInt32(bytes, 0);
    }

    public static bool Contains<T>(string enumstr)
    {
        try
        {
            var te= (T)Enum.Parse(typeof(T), enumstr);
            return true;
        }
        catch (Exception)
        {
            return false;
        }
        
    }
}
